package com.example.smartcity.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.ContentInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.smartcity.R;
import com.example.smartcity.activity.SSPDisplayActivity;
import com.example.smartcity.activity.VideoContentActivity;
import com.example.smartcity.bean.SSP;
import com.example.smartcity.bean.SSPBean;
import com.example.smartcity.bean.VideoBean;
import com.example.smartcity.utils.APIConfig;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class JDisplayFragment extends BaseFragment {
    private List<String> list = new ArrayList<>();
    private List<String> contenti = new ArrayList<>();
    private ListView jd_lv;
    private SSPBean sspBean;
    private String result;
    private static final String TAG = JDisplayFragment.class.getSimpleName();
    private Context context;

    @Override
    public View initView() {
        context = this.getContext();
        Log.i(TAG,"建党展示");
        initdata();
        View view = View.inflate(getContext(), R.layout.jd_display, null);
        jd_lv = view.findViewById(R.id.jd_lv);
        jd_lv.setAdapter(new JDisplayAdapter(getContext()));
        jd_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getContext(),"你点击了"+(position+1),Toast.LENGTH_LONG).show();
            }
        });
        return view;
    }

    @Override
    public void initData() {
        super.initData();

    }

    public class JDisplayAdapter extends BaseAdapter {
        private Context context;
        private LayoutInflater layoutInflater;

        public JDisplayAdapter(Context context) {
            this.context = context;
            layoutInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return sspBean != null ? sspBean.getSspList().size():0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // 实例化ViewHolder
            ViewHolder viewHolder = null;
            if (convertView==null){
                convertView = layoutInflater.inflate(R.layout.jd_display_item,null);
                // 生成一个ViewHolder对象
                viewHolder = new ViewHolder();
                // 绑定控件一3以便赋值
                viewHolder.jd_content = convertView.findViewById(R.id.jd_context);
                viewHolder.jd_date = convertView.findViewById(R.id.jd_date);
                viewHolder.jd_img = convertView.findViewById(R.id.jd_img);
                viewHolder.jd_title = convertView.findViewById(R.id.jd_title);
                // 把viewHolder传递进去
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            String[] arr = sspBean.getSspList().get(position).getImgnames().split(",");
            List<String> imglist = Arrays.asList(arr);
            Log.i("imgstring",imglist.get(0));
            viewHolder.jd_title.setText(sspBean.getSspList().get(position).getTitle());
            viewHolder.jd_content.setText(sspBean.getSspList().get(position).getContent());
            viewHolder.jd_date.setText("作者："+sspBean.getSspList().get(position).getAuthorname());
            //String url =  "https://pics6.baidu.com/feed/6609c93d70cf3bc75e6733e6d30f88a9cd112a68.jpeg?token=dc7266deb0b0fd98b39e60f0b0542334";
            Glide.with(context).load(APIConfig.BASE_URL+imglist.get(0)).into(viewHolder.jd_img);
            return convertView;
        }

        //写一个静态的class, 把layout_list_item的控件转移过来使用
         class ViewHolder{
            public TextView jd_title;
            public TextView jd_content;
            public TextView jd_date;
            public ImageView jd_img;
        }
    }
    public void initdata(){
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(APIConfig.BASE_URL + "/suiShouPai/download")
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("onFailure",e.getMessage());
            }

            @SuppressLint("LongLogTag")
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    result = response.body().string();
                    Log.i("videolist",result);
                    Gson gson = new Gson();
                    Type listType = new TypeToken<List<SSP>>() {}.getType();
                    sspBean =  gson.fromJson(result, SSPBean.class);
                    //String[] arr = sspList.get(position).getImgnames().split(",");
                    Log.i("imglist", sspBean.getSspList().get(1).getImgnames().split(",")+"");
                    //List<String> imglist = Arrays.asList(arr);;

                    Log.i("video", sspBean.getSspList().get(1).getTitle());
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            JDisplayAdapter videoListAdapter = new JDisplayAdapter(context);
                            jd_lv.setAdapter(videoListAdapter);
                            jd_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                    Intent intent = new Intent(getActivity(), SSPDisplayActivity.class);//todo
                                    intent.putExtra("authorid",sspBean.getSspList().get(i).getAuthorname());
                                    intent.putExtra("title",sspBean.getSspList().get(i).getTitle());
                                    intent.putExtra("imgs",sspBean.getSspList().get(i).getImgnames());
                                    intent.putExtra("content",sspBean.getSspList().get(i).getContent());
                                    getActivity().startActivity(intent);
                                }
                            });
                        }
                    });
                }
            }
        });
    }
    private static String cutContext6(String str) {
        if (str.length() > 6){
            String str1 = str.substring(0, 6) + "...";
            return str1;
        } else {
            return str;
        }
    }

}
